package com.lzs.collection.linkedlist;

// 移除错误的Node导入
public class LinkedList01 {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        // 模拟双向链表
        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node hsp = new Node("hsp");

        // 连接结点形成双链表
        jack.next = tom;
        tom.next = hsp;
        hsp.pre = tom;
        tom.pre = jack;

        Node first = jack; // 首结点
        Node last = hsp;   // 修正：尾结点应为hsp

        System.out.println("=====从头到尾遍历======");
        while (first != null) {
            System.out.println(first);
            first = first.next;
        }

        System.out.println("=====从尾到头遍历======");
        while (last != null) {
            System.out.println(last);
            last = last.pre;
        }

        // 在tom和hsp之间插入lzs
        Node lzs = new Node("lzs");
        lzs.next = hsp;
        hsp.pre = lzs;
        lzs.pre = tom;
        tom.next = lzs;

        // 重新初始化首尾指针
        first = jack;
        last = hsp;

        System.out.println("=====插入后从头到尾遍历======");
        while (first != null) {
            System.out.println(first);
            first = first.next;
        }

        System.out.println("=====插入后从尾到头遍历======");
        while (last != null) {
            System.out.println(last);
            last = last.pre;
        }
    }

    // 修正Node类定义，移除括号
    static class Node {
        public Object item;
        public Node next;
        public Node pre;

        public Node(Object item) {
            this.item = item;
        }

        public String toString() {
            return "Node name=" + item;
        }
    }
}